using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

// UI管理器
public partial class UIMgr : SingletonMono<UIMgr>
{
    private UIMgrBase mainCanvas;
    private UIMgrBase leftHandCanvas;
    private UIMgrBase rightHandCanvas;
    private UIMgrBase eyeHandCanvas;

    public override void Awake()
    {
        base.Awake();
        mainCanvas = new UIMgrBase();
        leftHandCanvas = new UIMgrBase();
        rightHandCanvas = new UIMgrBase();
        eyeHandCanvas = new UIMgrBase();

        bool enable = mainCanvas.OnInit("Canvas_Main");
        bool enable2 = leftHandCanvas.OnInit("XR Origin (XR Rig)/Camera Offset/Left Controller/Canvas_Left");
        bool enable3 = rightHandCanvas.OnInit("XR Origin (XR Rig)/Camera Offset/Right Controller/Canvas_Right");
        bool enable4 = rightHandCanvas.OnInit("XR Origin (XR Rig)/Camera Offset/Main Camera/Canvas_Eye");



        if (enable2 == false)
        {
            leftHandCanvas = mainCanvas;
        }
        if (enable3 == false)
        {
            rightHandCanvas = mainCanvas;
        }
        if (enable4 == false)
        {
            eyeHandCanvas = mainCanvas;
        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id">0:主UI  1: 左手UI  2:右手UI 3:头部 UI </param>
    /// <returns></returns>
    UIMgrBase GetUIMgrBase(int id)
    {
        switch (id)
        {
            case 0: return mainCanvas;
            case 1: return leftHandCanvas;
            case 2: return rightHandCanvas;
            case 3: return eyeHandCanvas;
            default: return null;
        }
    }


    public void ShowPanel_ClearLayer<T>(string panelName, E_UI_Layer layer, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        GetUIMgrBase(id)?.ShowPanel_ClearLayer<T>(panelName, layer, callBack);
    }
    public void ShowPanel_ClearLayer<T>(string panelName, Transform father = null, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        GetUIMgrBase(id)?.ShowPanel_ClearLayer<T>(panelName, father, callBack);
    }
    /// <summary>
    /// 打开面板 与上一个面版 不同 清除当前层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="panelName"></param>
    /// <param name="father"></param>
    /// <param name="callBack"></param>
    /// <param name="id"></param>
    public void ShowPanel_ClearLayer_diffpanel<T>(string panelName, Transform father = null, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        GetUIMgrBase(id)?.ShowPanel_ClearLayer_diffpanel<T>(panelName, father, callBack);
    }

    // 显示面板 
    public void ShowPanel<T>(string panelName, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        ShowPanel<T>(panelName, E_UI_Layer.Mid, callBack, id);
    }

    public void ShowPanel<T>(string panelName, E_UI_Layer layer = E_UI_Layer.Mid, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        GetUIMgrBase(id)?.ShowPanel<T>(panelName, layer, callBack);
    }
    public void ShowPanel<T>(string panelName, Transform father, UnityAction<T> callBack = null, int id = 0) where T : UICtrl
    {
        GetUIMgrBase(id)?.ShowPanel<T>(panelName, father, callBack);
    }



    // 得到某一个已经显示的面板，方便外部使用
    public T GetPanel<T>(string name, int id = 0) where T : UICtrl
    {
        return GetUIMgrBase(id)?.GetPanel<T>(name);
    }

    internal void ShowPanel<T>(string v, Action<object> p)
    {
        throw new NotImplementedException();
    }

    // 删除所有UI
    public void RemoveAll(int id = 0)
    {
        if (id == -1)
        {
            GetUIMgrBase(0)?.RemoveAll();
            GetUIMgrBase(1)?.RemoveAll();
            GetUIMgrBase(2)?.RemoveAll();
            GetUIMgrBase(3)?.RemoveAll();
        }
        else
        {
            GetUIMgrBase(id)?.RemoveAll();
        }

    }

    // 清空某一个节点的UI
    public void RemoveFromLayer(E_UI_Layer layer, int id = 0)
    {
        GetUIMgrBase(id)?.RemoveFromLayer(layer);
    }

    public void RemoveFromLayer(Transform father, int id = 0)
    {
        GetUIMgrBase(id)?.RemoveFromLayer(father);
    }


    // 删除面板
    public void HidePanel(string panelName, int id = 0)
    {
        GetUIMgrBase(id)?.HidePanel(panelName);
    }

    // 显示或不显示面板   已加载的列表
    public void ShowHidePanel(bool _show, string ignoreName = "UIMask", int id = 0)
    {
        GetUIMgrBase(id)?.ShowHidePanel(_show, ignoreName);
    }

    // 显示或不显示面板 单个
    public void ShowHidePanel(string panelName, bool _show, int id = 0)
    {
        GetUIMgrBase(id)?.ShowHidePanel(panelName, _show);
    }



    public void UIStackPush(GameObject panel, int id = 0)
    {
        GetUIMgrBase(id)?.UIStackPush(panel);
    }

    public void UIStackPush(string panelName, int id = 0)
    {
        GetUIMgrBase(id)?.UIStackPush(panelName);
    }

    public bool UIStackPop(int id = 0)
    {
        return GetUIMgrBase(id).UIStackPop();
    }

    public void UIStackRemove(string panelName, int id = 0)
    {
        GetUIMgrBase(id).UIStackRemove(panelName);
    }

    public void UIStackClear(int id = 0)
    {
        GetUIMgrBase(id).UIStackClear();
    }
}

